const a = require("./tree");

// console.log(a);
//运用递归和迭代两种方式实现二叉树的中序遍历

//递归
var middle = (root) => {
    let result = [];
    let inOrderTraversal = (node) => {
        if(node){
            inOrderTraversal(node.left);
            result.push(node.value);
            inOrderTraversal(node.right);
        }
    };
    inOrderTraversal(root);
    return result;
};
//迭代
var middle = (root) => {
    let res = [];
    let stack = [];
    let node = root;
    while(node || stack.length){
        while(node){
            stack.push(node);
            node = node.left;
        }
        node = stack.pop();
        res.push(node.value);
        node = node.right;
    }
    return res;
};
console.log(middle(a));
